package com.aitoedu.server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.aitoedu.server.dto.HomeworkDTO;
import com.aitoedu.server.dto.SetDTO;
import com.aitoedu.server.entity.Stestbank;
import com.aitoedu.server.entity.Testbank;
import com.aitoedu.server.mapper.ScMapper;
import com.aitoedu.server.mapper.StestbankMapper;
import com.aitoedu.server.mapper.TestbankMapper;
import com.aitoedu.server.utils.ReturnHandle;
import com.aitoedu.server.vo.QuestionVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aitoedu.server.entity.Ttestbank;
import com.aitoedu.server.service.TtestbankService;
import com.aitoedu.server.mapper.TtestbankMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 曹正豪
* @description 针对表【TTestBank(教师题库)】的数据库操作Service实现
* @createDate 2024-10-15 11:12:09
*/
@Service
@Slf4j
public class TtestbankServiceImpl extends ServiceImpl<TtestbankMapper, Ttestbank>
    implements TtestbankService{

    @Autowired
    private TestbankMapper testbankMapper;

    @Autowired
    private TtestbankMapper ttestbankMapper;

    @Autowired
    private StestbankMapper stestbankMapper;

    @Autowired
    private ScMapper scMapper;

    @Override
    public void importQuestion(List<Long> ids) {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Testbank> testBanks = testbankMapper.selectByIds(ids);
        List<Ttestbank> collect = testBanks.stream().map(t -> {
            Ttestbank ttestbank = new Ttestbank();
            BeanUtils.copyProperties(t, ttestbank);
            ttestbank.setCreateTime(LocalDateTime.now());
            ttestbank.setTid(tid);
            return ttestbank;
        }).collect(Collectors.toList());
        ttestbankMapper.insertQuestion(collect);
    }

    @Override
    public void set(List<SetDTO> setDTOList) {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<Ttestbank> collect = setDTOList.stream().map(s -> {
            Ttestbank ttestbank = new Ttestbank();
            BeanUtils.copyProperties(s, ttestbank);
            ttestbank.setTid(tid);
            ttestbank.setCreateTime(LocalDateTime.now());
            //为单选和多选题
            if(s.getOpts() != null) {
                ttestbank.setNumber(s.getOpts().size());
                ttestbank.setOpt(String.join("`",s.getOpts()));
                if(s.getType() == 2) ttestbank.setShortChoose(s.getShortChoose());
            }
            //为非论述
            if(s.getAnswers() != null) {
                ttestbank.setNumber(s.getAnswers().size());
                ttestbank.setAnswer(String.join("`",s.getAnswers()));
                if(s.getType() == 4) ttestbank.setEveryBlank(String.join("`",
                        s.getEveryBlank().stream().map(Object::toString).collect(Collectors.toList())));
            }
            return ttestbank;
        }).collect(Collectors.toList());
        ttestbankMapper.insert(collect);
    }

    @Override
    public void homework(HomeworkDTO homeworkDTO) {
        Stestbank stestbank = Stestbank.builder()
                .createTime(LocalDateTime.now())
                .chapterId(homeworkDTO.getChapterId())
                .stuStatus(2)
                .stuScore(0)
                .build();
        for(Long cid:homeworkDTO.getCourseIds()){
            List<Long> sids = scMapper.selectSidByCourseId(cid);
            for(Long sid:sids) stestbankMapper.homework(stestbank,sid,cid,homeworkDTO.getQuestionIds());
        }
    }

    @Override
    public List<QuestionVO> allQuestion() {
        Long tid = Long.valueOf(StpUtil.getLoginId().toString());
        List<QuestionVO> questionVOList = ReturnHandle.questionReturn(ttestbankMapper.getAllQuestion(tid));
        return questionVOList;
    }
}




